import java.util.concurrent.locks.*;

// Java 线程通信例子
public class Communication {
    public static void main(String[] args) {
        // S_Communication();
        L_Communication();
    }
    public static void L_Communication(){
        L_AorD l = new L_AorD();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    l.add();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"LA").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    l.delete();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"LB").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    l.add();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"LC").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    l.delete();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"LD").start();
    }
    
    public static void S_Communication(){
        S_AorD s = new S_AorD();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    s.add();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"SA").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    s.delete();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"SB").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    s.add();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"SC").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    s.delete();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"SD").start();
    }
}

// Synchronized
// 当出现多个线程时，wait() 会出现虚假唤醒，所以 wait 方法应该始终在循环中
// 也就是 wait 的条件应该是持续判断，而不是一次性判断
// wait 的睡眠会保存状态，下次唤醒继续执行
class S_AorD{
    private int num = 0;
    public synchronized void add() throws InterruptedException{
        // 优化，将 wait 放入循环
        while(num!=0){
            this.wait();
        }
        
        num ++;
        System.out.println(Thread.currentThread().getName()+" add");
        // 通知线程
        this.notifyAll();
            
    }
    public synchronized void delete() throws InterruptedException{
        while(num!=1){
            this.wait();
        }
        
        num --;
        System.out.println(Thread.currentThread().getName()+" delete");
        // 通知线程
        this.notifyAll();
    }
}
// Lock
class L_AorD{
    private int num = 0;
    private final ReentrantLock lock = new ReentrantLock();
    public final Condition condition = lock.newCondition();
    public void add() throws InterruptedException{
        lock.lock();
        System.out.println(Thread.currentThread().getName()+" lock");
        try{
            while(num != 0){
                System.out.println(Thread.currentThread().getName()+" wait");
                condition.await();
            }
            num ++;
            System.out.println(Thread.currentThread().getName()+" add");
            condition.signalAll();
        }
        finally{
            System.out.println(Thread.currentThread().getName()+" unlock");
            lock.unlock();
        }    
    }
    public void delete() throws InterruptedException{
        lock.lock();
        System.out.println(Thread.currentThread().getName()+" lock");
        try{
            while(num != 1){
                System.out.println(Thread.currentThread().getName()+" wait");
                condition.await();
            }
            num --;
            System.out.println(Thread.currentThread().getName()+" delete");
            condition.signalAll();
        }
        finally{
            System.out.println(Thread.currentThread().getName()+" unlock");
            lock.unlock();
        }    
    }
}
